package com.boruishangge.persagysdk.uitls;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;

import com.boruishangge.persagysdk.base.AppApplicationContext;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * Created by Administrator on 2015/11/5.
 */
public class PreferencesUtility {

    private static final String EMAIL = "email";
    private static PreferencesUtility sInstance;
    private static SharedPreferences mPreferences;

    public static final String START_PAGE_INDEX = "start_page_index";
    public static final String LOGIN_USERNAME = "loginUsername";
    public static final String LOGIN_PASSWORD = "loginPassword";

    public PreferencesUtility(Context context) {
        mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    }

    public static PreferencesUtility getInstance(final Context context) {
        if (sInstance == null) {
            sInstance = new PreferencesUtility(context.getApplicationContext());
        }
        return sInstance;
    }

    public void setOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
        mPreferences.registerOnSharedPreferenceChangeListener(listener);
    }

    public int getStartPageIndex() {
        return mPreferences.getInt(START_PAGE_INDEX, 0);
    }

    public void setStartPageIndex(final int index) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putInt(START_PAGE_INDEX, index);
        editor.apply();
    }

    public String getLoginUsername() {
        return mPreferences.getString(LOGIN_USERNAME, "");
    }

    public void setLoginUsername(final String loginUsername) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putString(LOGIN_USERNAME, loginUsername);
        editor.apply();
    }

    public String getLoginPassword() {
        return mPreferences.getString(LOGIN_PASSWORD, "");
    }

    public void setLoginPassword(final String loginPassword) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putString(LOGIN_PASSWORD, loginPassword);
        editor.apply();
    }

    public String getEmail() {
        return mPreferences.getString(EMAIL, "");
    }

    public void setEmail(final String email) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putString(EMAIL, email);
        editor.apply();
    }

    public String getSomething(String mark) {
        return mPreferences.getString(mark, "");
    }

    public Set<String> getCollection(String mark) {
        return new HashSet<>(Objects.requireNonNull(mPreferences.getStringSet(mark, new HashSet<>())));
    }

    public void setSomething(final String mark, final String something) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putString(mark, something);
        editor.apply();
    }

    public void setElementToSet(final String mark, String element) {
        final Set<String> set = getCollection(mark);
        set.add(element);
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putStringSet(mark, set);
        editor.apply();
    }

    public void setSetToSet(final String mark, Set<String> set) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putStringSet(mark, set);
        editor.apply();
    }

    public boolean getBoolean(String mark) {
        return mPreferences.getBoolean(mark, false);
    }

    public void setBoolean(final String mark, final boolean something) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putBoolean(mark, something);
        editor.apply();
    }

    public static boolean saveObject(String file, String ser) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = AppApplicationContext.application.openFileOutput(file, AppApplicationContext.application.MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(ser);
            oos.flush();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                oos.close();
            } catch (Exception e) {
            }
            try {
                fos.close();
            } catch (Exception e) {
            }
        }
    }

    public static String readObject(String file) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis =  AppApplicationContext.application.openFileInput(file);
            ois = new ObjectInputStream(fis);
            return (String) ois.readObject();
        } catch (FileNotFoundException e) {
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ois.close();
            } catch (Exception e) {
            }
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
        return null;
    }
}
